<?php

include('header.php');

if(isset($_REQUEST['submit'])) {
  if(isset($_REQUEST['link']) && isset($_REQUEST['description']) && isset($_REQUEST['tags'])) {
    $link = $_REQUEST['link'];
    $desc = $_REQUEST['description'];
    $tags = $_REQUEST['tags'];
    //$tags = explode(',', $tags);

    // get mysql connection

    $con = db_connect('links');
    $sql = "INSERT INTO tags(tags) VALUES ('$tags')";
    if (mysql_query($sql)) {
      $tags_id = mysql_insert_id($con);

    $sql2 = "INSERT INTO links(link, description, tags_id) VALUES('$link', '$desc', '$tags_id')";
    mysql_query($sql2) or die('Can not add link to database.');
    $msg = 1;
  }
}
}
?>
    <h1>Submit a Link</h1>
    <p>Fill in the form below to submit a link with a brief description and search tags.</p>

      <?php 
      if (isset($msg) && $msg==1) {
        echo '<div class="alert alert-success">Link submitted successfully!</div>';
        echo '<div class="alert alert-info"><a href="'.$link.'">'.$desc.'</a></div>';
      } else if (isset($msg) && $msg==2) {
        echo '<div class="alert alert-error">Link was not submitted!</div>';
      }
      ?>

      <form class="well" method="post" action="<?=$_SERVER['PHP_SELF'];?>">
        <label>Link URL</label>
        <input type="text" name="link" class="span6" placeholder="Put URL here...">
        <label>Description</label>
        <textarea name="description" class="span6" placeholder="A brief description of the link..."></textarea>
        <label>Search Tags for this link</label>
        <input type="text" name="tags" class="span6" placeholder="Comma separated tags...">
        <hr>
        <input type="submit" name="submit" value="Submit Link" class="btn-primary btn-large">
      </form>
      

    </div> <!-- /container -->

    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="assets/js/jquery.js"></script>
    <script src="assets/js/bootstrap-transition.js"></script>
    <script src="assets/js/bootstrap-alert.js"></script>
    <script src="assets/js/bootstrap-modal.js"></script>
    <script src="assets/js/bootstrap-dropdown.js"></script>
    <script src="assets/js/bootstrap-scrollspy.js"></script>
    <script src="assets/js/bootstrap-tab.js"></script>
    <script src="assets/js/bootstrap-tooltip.js"></script>
    <script src="assets/js/bootstrap-popover.js"></script>
    <script src="assets/js/bootstrap-button.js"></script>
    <script src="assets/js/bootstrap-collapse.js"></script>
    <script src="assets/js/bootstrap-carousel.js"></script>
    <script src="assets/js/bootstrap-typeahead.js"></script>

  </body>
</html>
